<?php
/**
 * Name: SmsLogModel.class.php
 * Description:
 * User: DIY
 * Date: 2016/9/24
 * Time: 14:38
 */

namespace Common\Model;


use Think\Model;

class SmsLogModel extends Model
{

    //自动完成
    protected $_auto = array (
        array('add_time','time',1,'function'),//新增
    );

    /*
     * 短信日志列表
     * @param data = array() 查询条件数据
     * @param $limit 查询起始条数
     * @return array
     * */
    public function getSmsLogList($data,$limit){
        //搜索数据
        $param = array(
            '1'=>'1',
        );
        if(isset($data['phone']) && $data['phone']!=''){
            $param['p1.phone'] = $data['phone'];
        }
        if(isset($data['type']) && $data['type']!=''){
            $param['p1.type'] = $data['type'];
        }
        if(isset($data['status']) && $data['status']!=''){
            $param['p1.status'] = $data['status'];
        }
        if(isset($data['start_date']) && $data['start_date']!=''){
            $param['p1.add_time'][] = array('gt',strtotime($data['start_date']));
        }
        if(isset($data['end_date']) && $data['end_date']!=''){
            $param['p1.add_time'][] = array('lt',strtotime($data['end_date']));
        }

        $list = $this->alias('p1')
            ->field('p1.*,p2.title')
            ->join('__SMS_TEMPLATE__ as p2 on p1.type=p2.type')
            ->where($param)
            ->limit($limit)
            ->order('id desc')
            ->select();

        return $list;
    }

    /*
     * 短信总和
     * @param $data = array() 查询参数数组
     * */
    public function getSmsLogCount($data = array()){
        $param = array(
            '1'=>'1',
        );
        if(isset($data['phone']) && $data['phone']!=''){
            $param['p1.phone'] = $data['phone'];
        }
        if(isset($data['type']) && $data['type']!=''){
            $param['p1.type'] = $data['type'];
        }
        if(isset($data['status']) && $data['status']!=''){
            $param['p1.status'] = $data['status'];
        }
        if(isset($data['start_date']) && $data['start_date']!=''){
            $param['p1.add_time'][] = array('gt',strtotime($data['start_date']));
        }
        if(isset($data['end_date']) && $data['end_date']!=''){
            $param['p1.add_time'][] = array('lt',strtotime($data['end_date']));
        }
        $count = $this->alias('p1')->where($param)->count();
        return $count;
    }

    /*
     *获取单条信息
     *@param id ID
     * */
    public function getSmsLogByID($id){

       $sms_log =  $this->alias('p1')
           ->field('p1.*,p2.title')
           ->join('__SMS_TEMPLATE__ as p2 on p1.type=p2.type')
           ->where(array('p1.id'=>$id))
           ->find();
        return $sms_log;
    }


}